package com.hsnn.medstgmini.util;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class Pdf2SwfUtils {
	
	/**
	 * @category 将pdf转化为swf
	 * @author zhouqi
	 * @param pdfToolsURL pdfToolsURL的安装路径
	 * @param pdfURL pdf的路径， 物理地址
	 * @param swfURL swf的路径，物理地址
	 * @return 
	 */
	public static void pdf2Swf(String pdfToosURL,String pdfURL, String swfURL){
		String commandStr = pdfToosURL+"\\pdf2swf.exe"+" -t "+pdfURL + " -s flashversion=9 -o " +swfURL;
		commandStr = commandStr.replaceAll("/","\\\\");
		exeCmd(commandStr);
	}
	
	/**
	 * @category 执行命令
	 * @author zhouqi
	 * @date 2015年9月15日10:03:26
	 * @param commandStr
	 */
	 public static void exeCmd(String commandStr) {

	        BufferedReader br = null;  
	        try { 	             
	            Process process = Runtime.getRuntime().exec(commandStr);
	            br = new BufferedReader(new InputStreamReader(process.getInputStream()));  
	            String line = null;  
	            StringBuilder sb = new StringBuilder();  
	            while ((line = br.readLine()) != null) {  
	                sb.append(line + "\n");  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (br != null) {  
	                try {  
	                    br.close();  
	                } catch (Exception e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	        }  
	 }
	 /**
	  * @category 将content转为pdf
	  * @param content content格式要注意
	  * @param targetUrl
	  * @throws Exception
	  */
	 public static void string2PDF(String fontPath,String content, String targetUrl)throws Exception{
	      Rectangle rectPageSize = new Rectangle(PageSize.A4);// 定义A4页面大小
	      Document document = new Document(rectPageSize, 50, 50, 50, 50);// 其余4个参数，设置了页面的4个边距
	      StyleSheet st = new StyleSheet();
		  st.loadTagStyle("body", "leading", "16,0");  
		  PdfWriter.getInstance(document, new FileOutputStream(targetUrl));  
	      BaseFont	baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	      Font fontChinese =  new  Font(baseFont , 12 , Font.NORMAL);
	      StringReader contentReader = new StringReader(content);
	      ArrayList elementList = HTMLWorker.parseToList(contentReader, st);
	      document.open();
	      for(int i = 0; i<elementList.size();i++){
	    	  Paragraph paragraph = (Paragraph)elementList.get(i);
	    	  String elementContent = paragraph.getContent();
	    	  Paragraph paragraph2 = new Paragraph(elementContent,fontChinese);
	    	  document.add(paragraph2);
	      }
	      document.close();  
	 }
}
